<?php

namespace common\modules\pms\models\backend;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * TaskSearch represents the model behind the search form about `common\modules\pms\models\backend\Task`.
 */
class TaskSearch extends Task
{

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'id',
                    'project_id',
                    'parent_id',
                    'assigner_id',
                    'start_time',
                    'end_time',
                    'created_at',
                    'updated_at',
                    'created_by',
                    'updated_by',
                ],
                'integer',
            ],
            [['name', 'label'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/pms', 'Task');
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Task::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if ( ! $this->validate())
        {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id'          => $this->id,
            'project_id'  => $this->project_id,
            'parent_id'   => $this->parent_id,
            'assigner_id' => $this->assigner_id,
            'start_time'  => $this->start_time,
            'end_time'    => $this->end_time,
            'created_at'  => $this->created_at,
            'updated_at'  => $this->updated_at,
            'created_by'  => $this->created_by,
            'updated_by'  => $this->updated_by,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'label', $this->label]);

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        return [

            [
                'attribute'     => 'id',
                'filterOptions' => [
                    'style' => 'width:90px',
                ],
            ],

            'project_id',

            'assigner_id',

            'name',

            'label',

            [
                'attribute' => 'start_time',
                'value'     => function ($model, $index)
                {
                    return date('Y-m-d', $model->start_time);
                },
            ],

            [
                'attribute' => 'end_time',
                'value'     => function ($model, $index)
                {
                    return date('Y-m-d', $model->end_time);
                },
            ],

            [
                'class' => 'common\widgets\grid\ActionColumn',
            ],
        ];
    }
}
